using System;
using Microsoft.SPOT;
using FusionWare.SPOT;
using Microsoft.SPOT.Hardware;
using FusionWare.SPOT.Hardware;

namespace $rootnamespace$
{
    /// <summary>Managed Driver for the emulated Simple SPIDevice</summary>
    /// <remarks>
    /// This demonstrates a managed code driver for an SPI Device. The device
    /// is the emulated FusionWare.SPOT.Emulator.SimpleSPIDevice. It has only 
    /// one 16 bit "Chip ID" register (or command/response if you like). 
    /// </remarks>
    partial class $safeitemname$ : SPIDeviceDriver
    {
        /// <summary>Creates a new device driver instance</summary>
        /// <param name="Bus">SPI bus module Id</param>
        /// <param name="ChipSelect">GPIO Pin for the Chip Select</param>
        /// <param name="ChipSelectActiveState">Active state of the chip select for this device</param>
        /// <remarks>
        /// Creates a new driver instance based on the settings provided. 
        /// For maximum isolation and encapsulation the application code only needs to
        /// provide the SPI bus and chip select pin information. The rest of the
        /// timing and clock information is specifc to the actual device capabilities
        /// and therefore provided internally in the managed driver rather than being
        /// supplied externally.
        /// </remarks>
        public $safeitemname$( SPI.SPI_module Bus, Cpu.Pin ChipSelectPin, bool ChipSelectActiveState )
        {
            SPI.Configuration cfg = new SPI.Configuration( ChipSelectPin
                                                         , ChipSelectActiveState
                                                         , 1
                                                         , 1
                                                         , false
                                                         , true
                                                         , 16000
                                                         , Bus
                                                         );
            base.Config = cfg;
        }

        /// <summary>Gets the Chip ID</summary>
        /// <value>Chip ID retrieved from the device</value>
        /// <remarks>
        /// This property will cache the ID so that it is actually read from the
        /// device only once. 
        /// </remarks>
        public ChipIDReg ChipID
        {
            get
            {
                if( this._ChipID.Value == 0 )
                    this._ChipID.Value = base.ReadReg16( (ushort)Registers.ChipID );

                return this._ChipID;
            }
        }
        private ChipIDReg _ChipID;
    }
}
